﻿using System;
using System.Linq;
using System.Collections.Generic;
using ControleRepresentanteSync.Application.DynamicSync;
using ControleRepresentanteSync.Application.Wizard;
using ControleRepresentanteSync.ControleRepresentanteServices;
using ControleRepresentanteSync.Logic;

namespace ControleRepresentanteSync.Application.Pedido
{
    class PedidoSelectionPresenter : DynamicSyncSelectionPresenter<IPedidoSyncViewModel, IPedidoViewModel, IPedidoListViewModel, Logic.Models.Pedido>
    {
        private List<IFabricaItem> m_Fabricas;
        private List<ITransportadoraItem> m_Transportadoras;
        private List<IVendedorItem> m_Vendedores;
        private List<IClienteItem> m_Clientes;
        private IEnumerable<ImpostoContract> m_Impostos;
        private IEnumerable<AttributeContract> m_Attributes;

        public PedidoSelectionPresenter(IPedidoSyncViewModel viewModel, IViewModelFactory viewModelFactory, IJolitexRetriever jolitexRetriever, WizardPageProvider pedidosPageProvider, IContractsProvider contractsProvider, IUserInterfaceService userInterfaceService) 
            : base(viewModel, pedidosPageProvider, viewModelFactory, jolitexRetriever, contractsProvider, userInterfaceService)
        {
        }

        protected override string Description
        {
            get { return "Selecione os pedidos que deseja sincronizar."; }
        }

        protected override string Title
        {
            get { return "Seleção de Pedidos"; }
        }

        protected override void CreateContentPresenter(IPedidoViewModel contentViewModel, Logic.Models.Pedido model)
        {
            new PedidoPresenter(contentViewModel, model,
                ViewModelFactory, GetAttributes(),
                GetSatelliteItems(ref m_Transportadoras, x => x.GetTransportadoras(), (i, c) => new TransportadoraItemPresenter(i, c)),
                GetSatelliteItems(ref m_Clientes, x => x.GetClientes(), (i, c) => new ClienteItemPresenter(i, c)),
                GetSatelliteItems(ref m_Vendedores, x => x.GetVendedores(), (i, c) => new VendedorItemPresenter(i, c)),
                GetSatelliteItems(ref m_Fabricas, x => x.GetFabricas(), (i, c) => new FabricaItemPresenter(i, c)));
        }

        private IEnumerable<ImpostoContract> GetImpostos()
        {
            return m_Impostos ?? (m_Impostos = ContractsProvider.GetImpostos());
        }

        private IEnumerable<AttributeContract> GetAttributes()
        {
            return m_Attributes ?? (m_Attributes = ContractsProvider.GetPedidoAttributes());
        }

        protected override IEnumerable<Logic.Models.Pedido> GetModels(out string errors)
        {
            return JolitexRetriever.GetPedidos(out errors).OrderBy(x => x.Numero);
        }

        public override void Dispose()
        {
            base.Dispose();

            FireOnClose(m_Clientes);
            FireOnClose(m_Fabricas);
            FireOnClose(m_Transportadoras);
            FireOnClose(m_Vendedores);
        }
    }
}